﻿/*******************************************************************************
 * Copyright © 2016
 * 
 * Description: MVC快速开发平台  FROM http://xmwxgn.com
 *
*********************************************************************************/
using ZFine.Code;
using ZFine.Domain.Entity.Gas;
using ZFine.Domain.IRepository.Gas;
using ZFine.Repository.Gas;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace ZFine.Application.Gas
{
    public class DispenserApp
    {
        private IDispenserRepository service = new DispenserRepository();


        /// <summary>
        /// 根据序列化获得摄像头
        /// </summary>
        /// <param name="DeviceSerialNo"></param>
        /// <returns></returns>
        public List<DispenserEntity> GetListByDeviceSerialNo(string DeviceSerialNo)
        {
            var expression = ExtLinq.True<DispenserEntity>();
            expression = expression.And(t => t.DeviceSerialNo.Trim() == DeviceSerialNo.Trim());
            return service.IQueryable(expression).ToList();
        }

        public DispenserEntity FindByDeviceSerialNo(string DeviceSerialNo)
        {
            return service.FindEntity(a=>a.DeviceSerialNo.Trim()== DeviceSerialNo.Trim());
        }

        public List<DispenserEntity> GetList(Pagination pagination, string keyword)
        {
            var expression = ExtLinq.True<DispenserEntity>();
            if (!string.IsNullOrEmpty(keyword))
            {
                expression = expression.And(t => t.DeviceSerialNo.Contains(keyword));
            }
            return service.FindList(expression, pagination);
        }
        public List<DispenserEntity> GetList( string keyword="")
        {
            var expression = ExtLinq.True<DispenserEntity>();
            if (!string.IsNullOrEmpty(keyword))
            {
                expression = expression.And(t => t.DeviceSerialNo.Contains(keyword));
            }
            return service.IQueryable(expression).OrderBy(t => t.DeviceSerialNo).ToList();
        }

        public IQueryable<DispenserEntity> GetIQueryable(string keyword = "")
        {
            var expression = ExtLinq.True<DispenserEntity>();
            if (!string.IsNullOrEmpty(keyword))
            {
                expression = expression.And(t => t.DeviceSerialNo.Contains(keyword));
            }
            return service.IQueryable(expression).OrderBy(t => t.DeviceSerialNo);
        }

        public DispenserEntity GetForm(string keyValue)
        {
            return service.FindEntity(keyValue);
        }
        public void DeleteForm(string keyValue)
        {
            service.Delete(t => t.F_Id == keyValue);
        }
        public void SubmitForm(DispenserEntity dispenserEntity, string keyValue)
        {
            //修改
            if (!string.IsNullOrEmpty(keyValue))
            {
                dispenserEntity.Modify(keyValue);
                service.Update(dispenserEntity);
            }
            //新增
            else
            {
                if (service.IQueryable().Where(t => t.IPAddress.Equals(dispenserEntity.IPAddress)).Count() > 0)
                {
                    throw new Exception("新增失败！IP地址已经存在。");
                }
                dispenserEntity.Create();
                service.Insert(dispenserEntity);
            }
        }
    }
}
